from Fingerprint.util import *
import datetime
from obspy import  read
from obspy.core import UTCDateTime
import numpy as np
from multiprocessing import Pool
import subprocess
import locale

def call_print(names):

    cmd ='python test_sub.py names'
    cmd = cmd.encode(locale.getdefaultlocale()[1])
    process = subprocess.Popen(cmd, stdout=subprocess.PIPE,shell=True)
    output,error = process.communicate()
    # string = str(output.stdout.readline(),encoding='gbk')
    # print(string)
    return output,error

# 遍历的各种方法
# 带行号遍历
seasons = ['Spring', 'Summer', 'Fall', 'Winter']
print(list(enumerate(seasons,start=2)))

for i,line in enumerate(seasons):
    print(i,line)


param_json = '../data/fp_input_CI_CDY_EHZ.json'
params = get_params(param_json)
# add this to end of time series of each partition so we don't have missing fingerprints
sec_extra = params['fingerprint']['spec_length'] + \
            (params['fingerprint']['fp_length'] - params['fingerprint']['fp_lag']) * \
            params['fingerprint']['spec_lag']
print(sec_extra)

time_extra = datetime.timedelta(seconds=sec_extra)
print(time_extra)

fname = '../data/Deci5.Pick.19991015130000.CI.CDY.EHZ.sac'
st = read(fname)
print(st)
starttime = datetime.datetime.strptime(str(st[0].stats.starttime), '%Y-%m-%dT%H:%M:%S.%fZ')
endtime = datetime.datetime.strptime(str(st[0].stats.endtime), '%Y-%m-%dT%H:%M:%S.%fZ')
print(starttime)
print(endtime)
print(endtime-starttime)
print(st[0].stats.endtime-st[0].stats.starttime)

dt = datetime.timedelta(seconds=86400)
print(dt)
s = starttime
print(st[0].stats.starttime)
print(UTCDateTime(s.strftime('%Y-%m-%dT%H:%M:%S.%f')))
# partition_st = st[0].slice(UTCDateTime(s.strftime('%Y-%m-%dT%H:%M:%S.%f')),UTCDateTime(e_padding.strftime('%Y-%m-%dT%H:%M:%S.%f')))
partition_st = st[0].slice(UTCDateTime(starttime),UTCDateTime(starttime+datetime.timedelta(seconds=74806)))
print(len(partition_st))
print(partition_st.data)


idx1 = np.array([1,5,9,13,17])
print(idx1)
idx2 = idx1+32
print(idx2)
fp_timestamp = np.asarray([int(np.mean((idx1[j], idx2[j]))) for j in range(len(idx1))])
print(fp_timestamp)

ss = np.zeros([3,2])
print(ss)

sss = np.zeros((3,2*2),dtype=bool)
print(sss)


name = ['雪下泪']
p = Pool(2)
# p.map(call_print , name)
print(p.map(call_print,name))

print(locale.getdefaultlocale())